/**
 * MusicDaoImp.java
 *
 *
 * $LastChangedBy:  $
 * $LastChangedDate:  $
 * $Revision:  $
 */
package com.myspace.dao.hibernate;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.myspace.dao.MusicDao;
import com.myspace.domain.Music;

public class MusicDaoImp extends GenericDaoImp<Music, Long> implements MusicDao {

	@SuppressWarnings("unchecked")
	public List<Music> findByYear(final Long userId, String year, String pageNo) {

		List<Music> ret = null;

		Calendar cal = Calendar.getInstance();
		try {
			int nYear = Integer.parseInt(year);
			cal.set(nYear, Calendar.JANUARY, 1, 0, 0, 0);
		} catch (Exception e) {
			cal.set(cal.get(Calendar.YEAR), Calendar.JANUARY, 1, 0, 0, 0);
		}
		final Date yearTime = cal.getTime();

		cal.add(Calendar.YEAR, 1);
		final Date nextYear = cal.getTime();

		int iPageNo2 = 1;
		try {
			iPageNo2 = Integer.parseInt(pageNo);
		} catch (NumberFormatException e) {
			iPageNo2 = 1;
		}
		final int iPageNo = iPageNo2;

		System.out.println("year:" + yearTime + " nextYear:" + nextYear);
		ret = (List) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) {
				Criteria crit = session.createCriteria(Music.class);
				crit.add(Restrictions.eq("userId", userId));
				if (yearTime != null)
					crit.add(Restrictions.between("createDate", yearTime,
							nextYear));
				crit.addOrder(Order.desc("createDate"));
				crit.setFirstResult((iPageNo - 1) * 10);
				crit.setMaxResults(10);
				return crit.list();
			};
		});
		return ret;
	}

	@SuppressWarnings("unchecked")
	public List<Music> findByCategory(final Long userId, final String category,
			String pageNo) {
		List<Music> ret = null;

		int iPageNo2 = 1;
		try {
			iPageNo2 = Integer.parseInt(pageNo);
		} catch (NumberFormatException e) {
			iPageNo2 = 1;
		}
		final int iPageNo = iPageNo2;

		System.out.println("category:" + category + " pageNo:" + pageNo);
		ret = (List) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) {
				Criteria crit = session.createCriteria(Music.class);
				crit.add(Restrictions.eq("userId", userId));
				if (category != null && !"".equals(category))
					crit.add(Restrictions.eq("category", category));
				crit.addOrder(Order.desc("createDate"));
				crit.setFirstResult((iPageNo - 1) * 10);
				crit.setMaxResults(10);
				return crit.list();
			};
		});
		return ret;
	}

	@SuppressWarnings("unchecked")
	public List<String> findAllCategory(Long userId) {
		List<String> ret = null;
		ret = getHibernateTemplate().find(
				"select distinct m.category from Music m where m.userId = ? ",
				userId);
		return ret;
	}

	public int findCountByYear(final Long userId, String year) {

		int ret;
		Calendar cal = Calendar.getInstance();
		try {
			int nYear = Integer.parseInt(year);
			cal.set(nYear, Calendar.JANUARY, 1, 0, 0, 0);
		} catch (Exception e) {
			cal.set(cal.get(Calendar.YEAR), Calendar.JANUARY, 1, 0, 0, 0);
		}
		final Date yearTime = cal.getTime();

		cal.add(Calendar.YEAR, 1);
		final Date nextYear = cal.getTime();

		System.out.println("year:" + yearTime + " nextYear:" + nextYear);
		ret = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) {
				Criteria crit = session.createCriteria(Music.class);
				crit.setProjection(Projections.rowCount());
				crit.add(Restrictions.eq("userId", userId));
				if (yearTime != null)
					crit.add(Restrictions.between("createDate", yearTime,
							nextYear));
				crit.addOrder(Order.desc("createDate"));
				return crit.uniqueResult();
			};
		});
		return ret;
	}

	public int findCountByCategory(final Long userId, final String category) {
		int ret;

		ret = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) {
				Criteria crit = session.createCriteria(Music.class);
				crit.setProjection(Projections.rowCount());
				crit.add(Restrictions.eq("userId", userId));
				if (category != null && !"".equals(category))
					crit.add(Restrictions.eq("category", category));
				crit.addOrder(Order.desc("createDate"));
				return crit.uniqueResult();
			};
		});
		return ret;
	}

	@SuppressWarnings("unchecked")
	public List<String> findAllYear(Long userId) {
		ArrayList<String> ret = new ArrayList<String>();
		List<Date> dates = getHibernateTemplate()
				.find(
						"select distinct m.createDate from Music m where m.userId = ? order by m.createDate desc",
						userId);
		Calendar calendar = Calendar.getInstance();
		for (Date date : dates) {
			calendar.setTime(date);
			Integer i = calendar.get(Calendar.YEAR);
			if (!ret.contains(i.toString()))
				ret.add(i.toString());
		}
		return ret;
	}

}
